package com.jn.erp.wechat.student.api.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.web.api.course.entity.TCourseOrder;
import com.jn.erp.web.api.course.entity.dto.TCourseOrderDto;
import com.jn.erp.web.api.course.entity.vo.TCourseOrderVo;
import com.jn.erp.web.api.course.service.TCourseOrderService;
import com.jn.erp.web.api.interaction.entity.TEvaluate;
import com.jn.erp.web.api.interaction.entity.vo.TEvaluateVo;
import com.jn.erp.web.api.interaction.service.TEvaluateService;
import com.jn.erp.web.api.student.entity.StudentClass;
import com.jn.erp.web.api.student.service.ClassService;
import com.jn.erp.wechat.common.api.controller.base.BaseWxAuthController;
import com.jn.erp.wechat.common.api.entity.dto.WxUserDto;
import com.jn.erp.wechat.core.WxTableDataInfo;
import com.jn.erp.wechat.student.api.entity.vo.StudentEvaluatedVo;
import com.jn.erp.wechat.student.api.entity.vo.StudentInfoVo;
import com.jn.erp.wechat.student.api.service.ClassInfoService;
import com.jn.erp.wechat.student.api.service.StudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <pre>
 *  成长档案
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/11/11 16:54
 */
@RestController
@RequestMapping("/wxs/growth")
public class WxsStudentGrowthController  extends BaseWxAuthController {


    @Autowired
    private TEvaluateService evaluateService;

    @Autowired
    private StudentInfoService studentInfoService;

    @Autowired
    private ClassInfoService classInfoService;

    //老师评价列表
    @GetMapping("/growthList")
    public WxTableDataInfo growthList(StudentEvaluatedVo studentEvaluatedVo){
        //获取登录家长端相关信息
        WxUserDto studentUser = getStudentUser();
        List<TEvaluate> list=new ArrayList<>();
        QueryWrapper<TEvaluate> qw=new QueryWrapper();
        qw.eq("type", 1);//老师评价学生
        qw.eq("is_delete", 0);//是否删除
        qw.eq("state", 0);//是否已评价 0 已评价
        if(StrUtil.isNotBlank(studentEvaluatedVo.getStudentId())&&!StrUtil.equals(studentEvaluatedVo.getStudentId(), "0")){
            qw.eq("t_id", studentEvaluatedVo.getStudentId());//学员id
        }else{
            //只显示在读报读学员
            List<StudentInfoVo> infos = studentInfoService.getStudentOrg(studentUser.getUserId());
            if(infos.size()>0){
                List<Long> collect = infos.stream().map(info -> Long.valueOf(info.getStudentId())).collect(Collectors.toList());
                qw.in("t_id", collect);
            }else{
                return getWxDataTable(list);
            }
        }
        qw.orderByDesc("create_date");
        startPage();
        list = evaluateService.list(qw);
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                TEvaluate e=list.get(i);
                TEvaluateVo tEvaluateVo=new TEvaluateVo();
                tEvaluateVo.setId(e.getId());
                tEvaluateVo.setOrgId(e.getOrgId());
                TEvaluate tEvaluate = evaluateService.evaluateGetById(tEvaluateVo);
                tEvaluate.setCreateDateShow(tEvaluate.getCreateDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                list.set(i,tEvaluate);
            }
        }
        //评分未实现
        return getWxDataTable(list);
    }

    //老师评价详情   {课程为排课课程}
    @PostMapping("/growthDetail")
    public AjaxResult growthDetail(@RequestBody TEvaluate evaluate){
        //获取登录家长端相关信息
        WxUserDto studentUser = getStudentUser();
        TEvaluateVo tEvaluateVo=new TEvaluateVo();
        tEvaluateVo.setId(evaluate.getId());
        tEvaluateVo.setOrgId(evaluate.getOrgId());
        TEvaluate evaluateDetail = evaluateService.getById(tEvaluateVo);
        evaluateDetail.setCreateDateShow(evaluateDetail.getCreateDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        TCourseOrderVo tco=new TCourseOrderVo();
        tco.setOrderNo(evaluateDetail.getOrderNo());
        TCourseOrderDto tCourseOrderDto= classInfoService.courseTimeInfoByOrder(tco);
        if(tCourseOrderDto!=null){
            evaluateDetail.setCourseOrderDto(tCourseOrderDto);
        }
        //关联评分未实现
        return success(evaluateDetail);
    }
}
